<template>
  <div>
    <my-header></my-header>
    <el-card class="card">
      <el-row :gutter="15">
        <el-col :span="13">
          <el-input
            placeholder="请输入内容"
            v-model="text"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <!-- <router-link :to="{name:'add'}"> -->
          <el-button type="primary" @click="goTo">添加用户</el-button>
          <!-- </router-link> -->
        </el-col>
      </el-row>

      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" label="#" width="180"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="180">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="180">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="180">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180">
          <template slot-scope="scope">{{scope.row.add_time   | formDate}}  </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[1, 5, 10, 20, 30, 40, 50]"
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGood } from "../../api/goods";
import myHeader from "../../components/myHeader.vue";
export default {
  components: { myHeader },
  data() {
    return {
      text: "",
      list: [],
      params: {
        query: this.text,
        pagenum: 1,
        pagesize: 10,
      },
      total: 1,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 添加
    goTo() {
      this.$router.push("/goods/add");
      // console.log(6532)
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.params.pagesize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.getList();
    },
    async getList() {
      let res = await getGood(this.params);
      console.log(res, "resGood");
      if (res.meta.status == 200) {
        this.list = res.data.goods;
        this.total = res.data.total;
      }
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index, row);
    },
    //   删除
    handleDelete(index, row) {
      console.log(index, row);
    },
  },
};
</script>

<style scoped>
.card {
  margin-top: 20px;
}
</style>